$(function() {
    var want = JSON.parse(localStorage.getItem('releaseWant'));
    // console.log($.type(want));
    var table = document.querySelector('.want');
    var strHTML = ``;
    for (var i = 0; i < want.length; i++) {
        strHTML += `<tr>
            <td><input name="wantItem" type="checkbox" value="${i}" /></td>
            <td>${want[i].productName}</td>
            <td>${want[i].wantPrice}</td>
            <td>${want[i].want_details}</td>
            <td>${want[i].tel} </td>
            <td class="del">删除</td>
                    </tr>`;
    }
    table.innerHTML = strHTML;
    checkOnclick(want); //全选按钮与复选按钮

    //点击删除按钮，删除本地存储数据
    var delIndex = -1; //需要删除的行的索引
    $('.del').click(function() {
        delIndex = $('.del').index($(this));
        this.parentNode.parentNode.removeChild(this.parentNode);
        want.splice(delIndex, 1);
        want.splice(delIndex, 1);
        localStorage.setItem('releaseWant', JSON.stringify(want));
        console.log(want);
    })
})

//全选按钮与复选按钮
function checkOnclick(want) {
    var all = document.getElementById('all');
    var wantItems = document.getElementsByName('wantItem');
    var sum = 0;
    all.onclick = function() {
        for (let i = 0; i < wantItems.length; i++) {
            wantItems[i].checked = all.checked;
        }
        if (all.checked) {
            sum = wantItems.length;
        } else {
            sum = 0;
        }
    }
    for (let i = 0; i < want.length; i++) {
        wantItems[i].onclick = function() {
            this.checked == true ? sum++ : sum--;
            if (sum == want.length) {
                all.checked = true;
            } else {
                all.checked = false;
            }
        }
    }
}